﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class SysManage_PubData : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetPager();
        }
    }

    private void SetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds = GetPubData();
        DataPager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = DataPager.PageSize;
        pds.CurrentPageIndex = DataPager.CurrentPageIndex - 1;
        PubData.DataSource = pds;
        PubData.DataBind();
    }

    private DataSet GetPubData()
    {
        string sqlstr = "SELECT data_code,data_name,data_type,order_num FROM u_pub_data_t WHERE if_valid='Y' ";
        if (Data_Type_I.Text.Trim().Length > 0)
        {
            sqlstr = sqlstr + " AND data_type like '%" + Tools.ReplaceString(Data_Type_I.Text.Trim(),50) + "%' ";
        }
        sqlstr = sqlstr + " ORDER BY data_type,order_num ";
        return OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, null);
    }

    protected void QueryData_Click(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void DaraPager_PageChanged(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void PubData_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string sqlstr = @"UPDATE u_pub_data_t SET data_name=:data_name,order_num=:order_num
                                   WHERE data_type=:data_type AND data_code =:data_code ";
        Label lt = PubData.Rows[e.RowIndex].FindControl("Data_Type_I") as Label;
        Label ld = PubData.Rows[e.RowIndex].FindControl("Data_Code_I") as Label;
        TextBox tn = PubData.Rows[e.RowIndex].FindControl("Data_Name_T") as TextBox;
        TextBox to = PubData.Rows[e.RowIndex].FindControl("Order_Num_T") as TextBox;
        OracleParameter[] _params = {
            new OracleParameter(":data_type",OracleType.VarChar,50),
            new OracleParameter(":data_code",OracleType.VarChar,20),
            new OracleParameter(":data_name",OracleType.VarChar,200),
            new OracleParameter(":order_num",OracleType.Int32)
        };
        _params[0].Value = lt.Text;
        _params[1].Value = ld.Text;
        _params[2].Value = tn.Text.Trim();
        _params[3].Value = to.Text.Trim();
        int res = OracleHelper.ExecuteNonQuery(sqlstr, CommandType.Text, _params);
        if (res > 0)
        {
            PubData.EditIndex = -1;
            SetPager();            
            JQueryAlert("数据更新成功!");
        }        
    }

    protected void PubData_RowEditing(object sender, GridViewEditEventArgs e)
    {
        PubData.EditIndex = e.NewEditIndex;
        SetPager();
    }

    protected void PubData_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "UPDATE u_pub_data_t SET if_valid = 'N'  WHERE data_type = :data_type AND data_code = :data_code";
        OracleParameter[] _params = {
                new OracleParameter(":data_type",OracleType.VarChar,50),
                new OracleParameter(":data_code",OracleType.VarChar,20)
            };
        Label lt = PubData.Rows[e.RowIndex].FindControl("Data_Type") as Label;
        Label ld = PubData.Rows[e.RowIndex].FindControl("Data_Code") as Label;
        _params[0].Value = lt.Text;
        _params[1].Value = ld.Text;
        int count = OracleHelper.ExecuteNonQuery(sqlstr, CommandType.Text, _params);        
        if (count > 0)
        {
            SetPager();
            JQueryAlert("数据删除成功!");
        }
    }

    protected void PubData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        PubData.EditIndex = -1;
        SetPager();
    }

    protected void BtnCreate_Click(object sender, EventArgs e)
    {
        if (InvalidData())
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "sysinfo"
                , "$(function(){$('#AddDataDiv').dialog('open');alert('该数据类型的数据代码已存在！');});", true);
            return;
        }
        string sqlstr = @"INSERT INTO u_pub_data_t(data_type,data_code,data_name,order_num) 
                                VALUES(:data_type,:data_code,:data_name,:order_num)";
        OracleParameter[] _params = { 
            new OracleParameter(":data_type",OracleType.VarChar,50),
            new OracleParameter(":data_code",OracleType.VarChar,20),
            new OracleParameter(":data_name",OracleType.VarChar,200),
            new OracleParameter(":order_num",OracleType.Int32)
        };
        _params[0].Value = Data_Type_A.Text.Trim();
        _params[1].Value = Data_Code_A.Text.Trim();
        _params[2].Value = Data_Name_A.Text.Trim();
        _params[3].Value = Int32.Parse(Order_Num_A.Text.Trim());
        int res = OracleHelper.ExecuteNonQuery(sqlstr, CommandType.Text, _params);
        if (res > 0)
        {
            SetPager();
            JQueryAlert("数据添加成功!");
        }        
    }

    private bool InvalidData()
    {
        string sqlstr = "Select count(1) from u_pub_data_t where data_type = :data_type AND data_code =:data_code AND if_valid = 'Y'";
        OracleParameter[] _params = {
            new OracleParameter(":data_type",OracleType.VarChar,50),
            new OracleParameter(":data_code",OracleType.VarChar,20)
        };
        _params[0].Value = Data_Type_A.Text.Trim();
        _params[1].Value = Data_Code_A.Text.Trim();
        int res = Int32.Parse(OracleHelper.ExecuteScalar(sqlstr, CommandType.Text, _params).ToString());
        return res > 0;
    }
}
